package com.mrjy.wechat.bo;

import com.mrjy.common.util.WechatSendTemplate;
import com.mrjy.wechat.conts.WxConsts;
import com.mrjy.wechat.dto.TemplateMessage;
import com.mrjy.wechat.process.AccessToken;
import com.mrjy.wechat.util.WeChatUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service("wechatTemplateBo")
public class WechatTemplateBo implements IWechatTemplateBo {

	@Override
	public Object send(TemplateMessage message,AccessToken accessToken) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == message){
			map.put("err_code", 40001);
			map.put("err_msg", "传入message为null");
			return map;
		}

		WechatSendTemplate wst = WechatSendTemplate.SEND_TEMPLATE_01;
		switch (message.getSendType()) {
		case "1":
			wst = WechatSendTemplate.SEND_TEMPLATE_01;
			break;
		case "2":
			wst = WechatSendTemplate.SEND_TEMPLATE_02;
			break;
		case "3":
			wst = WechatSendTemplate.SEND_TEMPLATE_03;
			break;
		case "4":
			wst = WechatSendTemplate.SEND_TEMPLATE_04;
			break;
		case "5":
			wst = WechatSendTemplate.SEND_TEMPLATE_05;
			break;
            case "6":
                wst = WechatSendTemplate.SEND_TEMPLATE_06;
        }
        message.setTemplateId(wst.getTemplateId());

		String sendUrl = WxConsts.getWeChatApiURL(WxConsts.POST_SEND_TEMPLATE,accessToken.getAccessToken());

        /**
         *  {"errcode":0,"errmsg":"ok","msgid":200228332}
		 */
//		WeChatUtil.sendWechatTemplateMessageIncome("");


		JSONObject jobj = WeChatUtil.httpRequest(sendUrl, WxConsts.HTTP_POST, message.toString());
		if(null != jobj &&  "0".equals(jobj.get("errcode").toString())){
			map.put("err_code", 200);
			map.put("err_msg", "发送消息模板成功");
			map.put("msg_id", jobj.get("msgid"));
		}else{
			map.put("err_code", 4002);
			map.put("err_msg", "发送消息模板不成功");
		}
		return map;
	}

	@Override
	public Object getAllTemplate(AccessToken accessToken) {
		Map<String, Object> map = new HashMap<String, Object>();
		String getTemplateUrl = WxConsts.getWeChatApiURL(WxConsts.GET_ALL_PRIVATE_TEMPLATE, accessToken.getAccessToken());
		JSONObject jobj = WeChatUtil.httpRequest(getTemplateUrl, WxConsts.HTTP_GET, null);
		if(null != jobj){
			map.put("err_code", 200);
			map.put("err_msg", "获取模板列表成功");
			map.put("templates", jobj);
		}else{
			map.put("err_code", 4000);
			map.put("err_msg", "获取模板列表成功");
		}

        return map;
    }

	@Override
	public Object getTeaplate(AccessToken accessToken, String templateIdShort) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isBlank(templateIdShort)){
			map.put("err_code", 40001);
			map.put("err_msg", "传入模板ID为null");
			return map;
		}

        String getTemplateUrl = WxConsts.getWeChatApiURL(WxConsts.API_ADD_TEMPLATE, accessToken.getAccessToken());
        JSONObject jobj = WeChatUtil.httpRequest(getTemplateUrl, WxConsts.HTTP_POST, templateIdShort);
		if(null != jobj && "0".equals(jobj.get("errcode"))){
			map.put("err_code", 200);
			map.put("err_msg", "获取模板信息成功");
			map.put("template", jobj);
		}else{
			map.put("err_code", 4002);
			map.put("err_msg", "获取模板信息失败");
			map.put("template", null);
		}

        return map;
    }

}
